{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "模型基本训练_保存_下载.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yejianfeng2014/AI/blob/master/%E6%A8%A1%E5%9E%8B%E5%9F%BA%E6%9C%AC%E8%AE%AD%E7%BB%83_%E4%BF%9D%E5%AD%98_%E4%B8%8B%E8%BD%BD.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "he9Ee2-XtL1Z",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "挂载云硬盘，如果验证数据请填写验证信息 挂载硬盘，**只需要挂载一次**"
      ]
    },
    {
      "metadata": {
        "id": "hK5MYRMbf12-",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!apt-get install -y -qq software-properties-common python-software-properties module-init-tools\n",
        "!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null\n",
        "!apt-get update -qq 2>&1 > /dev/null\n",
        "!apt-get -y install -qq google-drive-ocamlfuse fuse\n",
        "from google.colab import auth\n",
        "auth.authenticate_user()\n",
        "from oauth2client.client import GoogleCredentials\n",
        "creds = GoogleCredentials.get_application_default()\n",
        "import getpass\n",
        "!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL\n",
        "vcode = getpass.getpass()\n",
        "!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "7yzpLX_atbFH",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "#训练一个基本模型，采用mnist 数据集，训练了一个模型\n",
        "## 关键点，实现了模型的保存到云网盘上\n",
        "\n",
        "1.     model_path = \"./test/model.ckpt\" 核 必须以‘./’ 开头，就会保存在云盘的根目录上。然后第二个test会创建一个目录把模型直接保存进去列表项\n",
        "2.  然后选择文件 ->保存 会自动保存在云网盘下test的目下，就可以在云网盘里面下载\n",
        "    "
      ]
    },
    {
      "metadata": {
        "id": "5UAx2aOFjBwc",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 89
        },
        "outputId": "6176f8e7-9fb0-428a-da73-a5c0a5923064"
      },
      "cell_type": "code",
      "source": [
        "from __future__ import print_function\n",
        "\n",
        "# Import MNIST data\n",
        "from tensorflow.examples.tutorials.mnist import input_data\n",
        "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n",
        "\n",
        "import tensorflow as tf\n",
        "\n",
        "# Parameters\n",
        "learning_rate = 0.001\n",
        "batch_size = 100\n",
        "display_step = 1\n",
        "model_path = \"./test/model.ckpt\"\n",
        "\n",
        "# Network Parameters\n",
        "n_hidden_1 = 256 # 1st layer number of features\n",
        "n_hidden_2 = 256 # 2nd layer number of features\n",
        "n_input = 784 # MNIST data input (img shape: 28*28)\n",
        "n_classes = 10 # MNIST total classes (0-9 digits)\n",
        "\n",
        "# tf Graph input\n",
        "x = tf.placeholder(\"float\", [None, n_input])\n",
        "y = tf.placeholder(\"float\", [None, n_classes])\n",
        "\n"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
            "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
            "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
            "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "fCEYnXR2lDob",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 181
        },
        "outputId": "6381980a-537d-45fd-d938-419a22708b10"
      },
      "cell_type": "code",
      "source": [
        "\n",
        "# Create model\n",
        "def multilayer_perceptron(x, weights, biases):\n",
        "    # Hidden layer with RELU activation\n",
        "    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])\n",
        "    layer_1 = tf.nn.relu(layer_1)\n",
        "    # Hidden layer with RELU activation\n",
        "    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])\n",
        "    layer_2 = tf.nn.relu(layer_2)\n",
        "    # Output layer with linear activation\n",
        "    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']\n",
        "    return out_layer\n",
        "\n",
        "# Store layers weight & bias\n",
        "weights = {\n",
        "    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),\n",
        "    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n",
        "    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))\n",
        "}\n",
        "biases = {\n",
        "    'b1': tf.Variable(tf.random_normal([n_hidden_1])),\n",
        "    'b2': tf.Variable(tf.random_normal([n_hidden_2])),\n",
        "    'out': tf.Variable(tf.random_normal([n_classes]))\n",
        "}\n",
        "\n",
        "# Construct model\n",
        "pred = multilayer_perceptron(x, weights, biases)\n",
        "\n",
        "# Define loss and optimizer\n",
        "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n",
        "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n",
        "\n",
        "# Initialize the variables (i.e. assign their default value)\n",
        "init = tf.global_variables_initializer()\n"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:From <ipython-input-3-9436529ef4bb>:28: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "\n",
            "Future major versions of TensorFlow will allow gradients to flow\n",
            "into the labels input on backprop by default.\n",
            "\n",
            "See `tf.nn.softmax_cross_entropy_with_logits_v2`.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "mRDpKYZQlLgX",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "f0b5aa45-6323-47ae-c702-4121bee2e1a2"
      },
      "cell_type": "code",
      "source": [
        "saver = tf.train.Saver()\n",
        "print(\"Starting 1st session...\")\n"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Starting 1st session...\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "kuxvOHBglQ1w",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 449
        },
        "outputId": "6ed4f6f0-cb04-404b-bbfa-a8d5dbca80d3"
      },
      "cell_type": "code",
      "source": [
        "with tf.Session() as sess:\n",
        "\n",
        "    # Run the initializer\n",
        "    sess.run(init)\n",
        "\n",
        "    # Training cycle\n",
        "    for epoch in range(20):\n",
        "        avg_cost = 0.\n",
        "        total_batch = int(mnist.train.num_examples/batch_size)\n",
        "        # Loop over all batches\n",
        "        for i in range(total_batch):\n",
        "            batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
        "            # Run optimization op (backprop) and cost op (to get loss value)\n",
        "            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,\n",
        "                                                          y: batch_y})\n",
        "            # Compute average loss\n",
        "            avg_cost += c / total_batch\n",
        "        # Display logs per epoch step\n",
        "        if epoch % display_step == 0:\n",
        "            print(\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n",
        "                \"{:.9f}\".format(avg_cost))\n",
        "    print(\"First Optimization Finished!\")\n",
        "\n",
        "    # Test model\n",
        "    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n",
        "    # Calculate accuracy\n",
        "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
        "    print(\"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))\n",
        "\n",
        "    # Save model weights to disk\n",
        "    save_path = saver.save(sess, model_path)\n",
        "    print(\"Model saved in file: %s\" % save_path)\n",
        "\n",
        "# Running a new session\n",
        "print(\"Starting 2nd session...\")"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch: 0001 cost= 148.555580732\n",
            "Epoch: 0002 cost= 37.329880381\n",
            "Epoch: 0003 cost= 23.441558777\n",
            "Epoch: 0004 cost= 16.364971469\n",
            "Epoch: 0005 cost= 12.042239525\n",
            "Epoch: 0006 cost= 8.769514230\n",
            "Epoch: 0007 cost= 6.625274057\n",
            "Epoch: 0008 cost= 4.867614367\n",
            "Epoch: 0009 cost= 3.569638941\n",
            "Epoch: 0010 cost= 2.792400248\n",
            "Epoch: 0011 cost= 2.008744070\n",
            "Epoch: 0012 cost= 1.505613955\n",
            "Epoch: 0013 cost= 1.182687648\n",
            "Epoch: 0014 cost= 0.903851427\n",
            "Epoch: 0015 cost= 0.697083753\n",
            "Epoch: 0016 cost= 0.642139443\n",
            "Epoch: 0017 cost= 0.567303761\n",
            "Epoch: 0018 cost= 0.435636582\n",
            "Epoch: 0019 cost= 0.440558042\n",
            "Epoch: 0020 cost= 0.324702794\n",
            "First Optimization Finished!\n",
            "Accuracy: 0.9528\n",
            "Model saved in file: ./test/model.ckpt\n",
            "Starting 2nd session...\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "W8l6ijdrx_Ey",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "使用以下方法一次可以下载一个文件下来，其他几个也可以采用类似的方法下载。\n",
        "我还是建议在云网盘里面使用客户端下载，毕竟网页下载很慢。"
      ]
    },
    {
      "metadata": {
        "id": "cIX7iLwEla2N",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from google.colab import files\n",
        "files.download('./test/model.ckpt.index') \n",
        "\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "k4xcESS0l1KH",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}